package flc.ast.activity;

import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.Image;
import flc.ast.dataBase.AppDatabase;
import flc.ast.databinding.ActivityImageDetailBinding;
import flc.ast.dialog.AddLabelDialog;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.IntentUtil;

public class ImageDetailActivity extends BaseAc<ActivityImageDetailBinding> {

    public static Image image;

    @Override
    protected int onCreate() {
        return R.layout.activity_image_detail;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivImageDetailBack.setOnClickListener(v -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvDelete.setOnClickListener(this);
        mDataBinding.tvLabel.setOnClickListener(this);
        mDataBinding.llCollect.setOnClickListener(this);
        mDataBinding.tvRemove.setOnClickListener(this);
        mDataBinding.tvShare.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvDelete || id == R.id.tvRemove) {//删除/移出
            AppDatabase.getInstance(mContext).albumDao().deleteImage(image);
            ToastUtils.showShort(getString(R.string.delete_suc));
            finish();
        } else if (id == R.id.tvLabel) {//标签
            showAddLabelDialog();
        } else if (id == R.id.llCollect) {//收藏
            image.isFavorite = !image.isFavorite;
            AppDatabase.getInstance(mContext).albumDao().updateImage(image);
            isCollect();
        } else if (id == R.id.tvShare) {//分享
            IntentUtil.shareImage(mContext, "", image.path);
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(image.path).into(mDataBinding.ivImg);
    }

    private void showAddLabelDialog() {
        AddLabelDialog dialog = new AddLabelDialog(mContext);
        dialog.setName(image.tags);
        dialog.setListener(new AddLabelDialog.Listener() {
            @Override
            public void clickConfirm(String name) {
                image.tags = name;
                AppDatabase.getInstance(mContext).albumDao().updateImage(image);
            }
        });
        dialog.show();
    }

    private void isCollect() {
        mDataBinding.ivCollect.setImageResource(image.isFavorite ? R.drawable.aysc : R.drawable.awsc);
    }

}